package com.lln.controller;

import com.lln.pojo.Result;
import com.lln.pojo.po.EmployeePo;
import com.lln.pojo.vo.EmployeeDetailVo;
import com.lln.pojo.vo.EmployeeVo;
import com.lln.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @GetMapping
    public Result findAll() {
        List<EmployeePo> employeePoList = employeeService.findAll();
        List<EmployeeVo> employeeVoList = new ArrayList<>();
        for (EmployeePo employeePo : employeePoList) {
            EmployeeVo employeeVo = new EmployeeVo();
            BeanUtils.copyProperties(employeePo, employeeVo);
            employeeVo.setLabel(employeePo.getName());
            employeeVoList.add(employeeVo);
        }
        return Result.success(employeeVoList);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        EmployeePo employeePo = employeeService.findById(id);
        EmployeeDetailVo detailVo = new EmployeeDetailVo();
        BeanUtils.copyProperties(employeePo,detailVo);
        return Result.success(detailVo);
    }
}
